package cn.ai.shoes.core.handler;


import cn.ai.shoes.common.pojo.entity.CommonResult;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import java.io.IOException;

import static cn.ai.shoes.common.exception.constant.GlobalErrorCodeConstants.ACCOUNT_OTHER_LOGIN;


public class MySessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        //将result象转换为json字符串
        String json= JSON.toJSONString(CommonResult.error(ACCOUNT_OTHER_LOGIN));
        //获取响应对象
        HttpServletResponse response = event.getResponse();
        //设置响应头
        response.setContentType("application/json;charset=utf-8");
        //设置响应体
        response.getWriter().print(json);
    }
}
